//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  remindTime:0,
  clockShow:false,
  clockHeight:0,
  mTime:10000,
  timeStr:'01:00',
  rate:'',
  timer:null,
  audio: null,
  data: {
    time: '1',
    cateArr:[
      {
        icon:'work',
        text:'工作'
      },
      {
        icon:'study',
        text:"学习",
      },
      {
        icon:'think',
        text:'思考'
      },
      {
        icon:'write',
        text:'写作'
      },
      {
        icon:'sport',
        text:'运动'
      },
      {
        icon:'read',
        text:"阅读"
      }
    ],
    cateActive:'0',
    okShow:false,
    pauseShow:true,
    continueCancleShow:false,
    val:'',
    task_list_data:[],
  },
  //task_data
 
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var res = wx.getSystemInfoSync();
    var rate = 750 / res.windowWidth;
    this.setData({
      rate: rate,
      clockHeight: rate * res.windowHeight
    })
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  slideChange: function (e) {
    this.setData({
      time : e.detail.value
    })
  },
  clickCate:function (e) {
    this.setData({
      cateActive: e.currentTarget.dataset.index
    })
  },
  start: function() {
    this.setData({
      clockShow:true, 
      mTime: this.data.time*60*1000,
      remindTime: this.data.time*60*1000,
      timeStr:parseInt(this.data.time) >= 10 ? this.data.time+':00' : '0'+this.data.time +':00'
    })
    this.drawBg();
    var that = this;
    var n = 5;
    if(this.data.time <= 25) {
      that.drawActive();
    } else {
      that.drawActive();
      setTimeout(function() {
        that.pause();
        that.drawActiveBreak25(n);
        setTimeout(function() {
        that.continue();
      }, n * 60 * 1000);
      }, 25 * 60 * 1000);
     
    }   
  },
  drawBg:function () {
    var lineWidth = 6 / this.data.rate; // px
    var ctx =wx.createCanvasContext('progress_bg');
    ctx.setLineWidth(lineWidth);
    ctx.setStrokeStyle('#000000');
    ctx.setLineCap('round');
    ctx.beginPath();
    ctx.arc(400/this.data.rate/2,400/this.data.rate/2,400/this.data.rate/2 - 2* lineWidth,0,2*Math.PI,false);
    ctx.stroke();
    ctx.draw();
  },
  drawActive: function () {
    var _this = this;
    var timer = setInterval(function () {
      var angle = 1.5 + 2*(_this.data.time*60*1000 - _this.data.mTime)/(_this.data.time*60*1000);
      var currentTime = _this.data.mTime - 100
      _this.setData({
          mTime: currentTime
      });
      if(angle < 3.5) {
        if(currentTime % 1000 == 0) {
          var timeStr1 = currentTime / 1000; // s
          var timeStr2 = parseInt(timeStr1 / 60) // m
          var timeStr3 = (timeStr1 - timeStr2 * 60) >= 10 ? (timeStr1 - timeStr2 * 60) : '0' + (timeStr1 - timeStr2 * 60);
          var timeStr2 = timeStr2 >= 10 ? timeStr2:'0'+timeStr2;
          _this.setData({
            timeStr:timeStr2 + ':' + timeStr3
          })
        }
        var lineWidth = 6 / _this.data.rate; // px
        var ctx = wx.createCanvasContext('progress_active');
        ctx.setLineWidth(lineWidth);
        ctx.setStrokeStyle('#ffffff');
        ctx.setLineCap('round');
        ctx.beginPath();
        ctx.arc(400 / _this.data.rate / 2, 400 / _this.data.rate / 2, 400 / _this.data.rate / 2 - 2 * lineWidth, 1.5 * Math.PI, angle * Math.PI, false);
        ctx.stroke();
        ctx.draw();
      } else {
        var logs = wx.getStorageSync('logs') || [];
        if (_this.data.time != undefined && _this.data.time > 0) {
          logs.unshift({
            date: util.formatTime(new Date),
            cate: _this.data.cateActive,
            time: _this.data.time
          });
        }
        wx.setStorageSync('logs', logs);
        _this.setData({
          timeStr:'00:00',
          okShow:true,
          pauseShow: false,
          continueCancleShow: false        
        })
        clearInterval(timer);
        _this.playAudioHun(_this);
      }
    },100);
    _this.setData({
      timer:timer
    })
  },
  pause : function() {
    clearInterval(this.data.timer);
    this.setData({
      pauseShow: false,
      continueCancleShow: true,
      okShow: false
    })
  },
  continue: function () {
    this.drawActive();
    this.setData({
      pauseShow: true,
      continueCancleShow: false,
      okShow: false
    })    
  },
  cancle: function (){
    clearInterval(this.data.timer);
    this.message('不要轻言放弃哦~');
    this.setData({
      pauseShow: true,
      continueCancleShow: false,
      okShow: false,
      clockShow:false      
    })
  },
  ok: function () {
    clearInterval(this.data.timer);
    this.setData({
      pauseShow: true,
      continueCancleShow: false,
      okShow: false,
      clockShow: false
    }),
    this.data.audio.stop();
    if (parseInt(this.data.remindTime) >= 40*60*1000) {
        this.message('请及时注意休息哦')
    } else {
      this.message('按时完成了任务，继续加油！')
    }
  },
  playAudioHun: function(obj) {
    const audioP = wx.createInnerAudioContext();
    audioP.src = '/audio/audio.mp3';
    audioP.play();
    obj.setData({
      audio: audioP
    })
  },
  message: function(info) {
      wx.showToast({
        title: info,
        icon: 'none',
        duration: 1500
      })
  },
  // Todo list
  getval(e){
    this.setData({ val: e.detail.value})
  },
  add() {
    var data1 = this.data.task_list_data;
    if (this.data.val == '') {
      this.message("不能添加空任务")
      return
    }
    data1.push(this.data.val)
    this.setData({
      task_list_data:data1,
      val:''})
  },
  del(e) {
    var i = e.target.dataset.index;
    var data2 = this.data.task_list_data;
    data2.splice(i, 1)
    this.setData({
      task_list_data: data2
    })
  },
  start_task() {
      this.start();
  },
  drawActiveBreak25: function (n) {
    var _this = this;
    var breakTime = parseInt(n);
    var mbreakTime = breakTime * 60 * 1000;
    var currentTime = mbreakTime;
    var timer = setInterval(function () {
      var angle = 1.5 + 2*(breakTime*60*1000 - mbreakTime)/(breakTime*60*1000);
      currentTime = mbreakTime - 100
      mbreakTime = currentTime;
      if(angle < 3.5) {
        if(currentTime % 1000 == 0) {
          var timeStr1 = currentTime / 1000; // s
          var timeStr2 = parseInt(timeStr1 / 60) // m
          var timeStr3 = (timeStr1 - timeStr2 * 60) >= 10 ? (timeStr1 - timeStr2 * 60) : '0' + (timeStr1 - timeStr2 * 60);
          var timeStr2 = timeStr2 >= 10 ? timeStr2:'0'+timeStr2;
          _this.setData({
            timeStr:timeStr2 + ':' + timeStr3
          })
        }
        var lineWidth = 6 / _this.data.rate; // px
        var ctx = wx.createCanvasContext('progress_active');
        ctx.setLineWidth(lineWidth);
        ctx.setStrokeStyle('#ffffff');
        ctx.setLineCap('round');
        ctx.beginPath();
        ctx.arc(400 / _this.data.rate / 2, 400 / _this.data.rate / 2, 400 / _this.data.rate / 2 - 2 * lineWidth, 1.5 * Math.PI, angle * Math.PI, false);
        ctx.stroke();
        ctx.draw();
      } else {
        clearInterval(timer);
      }
    },100);
    _this.setData({
      continueCancleShow: false,
    })
  },
})
